package com.sofwin.configer;

import com.sofwin.realm.MyRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @packageName: com.sofwin.configer
 * @user: andyliu
 * @date: 2023/1/5 16:41
 * @email 115176513@qq.com
 * @description: TODO
 */
@Configuration
@Component
public class ShiroConfiger {

    @Autowired
    private MyRealm realm;

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager());
        bean.setLoginUrl("/index.jsp");
        bean.setUnauthorizedUrl("/login");
        bean.setFilterChainDefinitions("/** = anon");
        return bean;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);
        return manager;
    }
}
